home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / sgml / unix / sgmlh / apimodel.src < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-03  |  8.6 KB  |  226 lines

  1. /******************************************************************************/
  2. #include "sgmlapi.h"          /* SGML application programming interface. */
  3. /******************************************************************************/
  4. /* APIMODEL.C: Model program to demonstrate use of SGMLAPI.
  5.    This program merely prints information as it is returned
  6.    by the API.  However the shell can be used as a model for
  7.    your own text processor or dedicated application.
  8. */
  9. static void docproc(void);    /* Main loop for processing document. */
  10. static void getatts(void);    /* Get attribute list (tag or data). */
  11. static void proctkn(unsigned, char *); /* Process attribute token by type. */
  12. static void getde(void);      /* Get data entity control block. */
  13. /******************************************************************************/
  14. /*lint -e528                     Suppress unreferenced function messages. */
  15. void main(int ,char * *); /* Main function. */
  16. void main(argc, argv)
  17. int argc;
  18. char **argv;
  19. {
  20. /*lint +e528                     Restore unreferenced function messages. */
  21.      char *fileid = 0;        /* Primary file identifier. */
  22.      int i;                   /* Loop control. */
  23.      unsigned msgcnt;         /* Number of messages issued by parser. */
  24.  
  25.      /* PARSE ARGUMENTS
  26.      */
  27.      /* SGMLOPT handles common parser options.  We handle the other args. */
  28.      sgmloset();              /* Initialize for SGML option processing. */
  29.      for (i = 0; ++i<argc;) {
  30.           if (argv[i][0]!='/') fileid = argv[i];
  31.           else if (sgmlopt(argv[i])!=0) {
  32.                printf(
  33. "\nTP001-> \"%s\" option switch is incorrect; usage is: %s",
  34.                argv[i], sgmlomsg);
  35.                exit(001);
  36.           }
  37.      }
  38.      if (fileid==0 || *fileid=='?') {
  39.           printf(
  40. "\nTP002-> No primary file specified; usage is: %s [profile;]file",
  41.           sgmlomsg);
  42.           exit(002);
  43.      }
  44.      /* IDENTIFY THE SGML DOCUMENT ENTITY
  45.      */
  46.      /* Define specified external file(s) as document entity. */
  47.      if (docent(fileid)) {
  48.           printf(
  49. "\nTP005-> SGML document entity system ID (%s) exceeds 240 characters.",
  50.           fileid);
  51.           exit(005);
  52.      }
  53.      /* INITIALIZE THE RUN
  54.      */
  55.      sgmlset();                    /* Initialize the SGML parser. */
  56.      while (++te.pass<=te.passes) {
  57.           /* INITIALIZE THE PASS
  58.           */
  59.           if (sgmlpset()) {
  60.                printf(
  61. "\nTP006-> Could not open SGML document entity in file: %s", fileid);
  62.                exit(006);
  63.           }
  64.           /* RUN THE PASS
  65.           */
  66.           printf(
  67. "\nTP105-> Pass %d of %d.", te.pass, te.passes);
  68.           docproc();
  69.      }
  70.      /* END OF RUN: Report message count and capacity statistics.
  71.      */
  72.      msgcnt = sgmlend();
  73.      printf(
  74. "\nTP106-> Processing completed; %u messag%s issued.",
  75.      msgcnt, (msgcnt==1) ? "e" : "es");
  76.      exit(0);
  77. }
  78. /******************************************************************************/
  79. /* DOCPROC: Process the returns from SGMLNEXT.
  80. */
  81. void docproc()
  82. {
  83.      int rc;                  /* Return code from SGMLNEXT. */
  84.  
  85.      while ((rc = sgmlnext())!=TPEOD) switch (rc) {
  86.      case TPNDATA:            /* Non-SGML data entity reference. */
  87.           location(10);
  88.           printf("Non-SGML data entity %s", de.ename);
  89.           getde();
  90.           continue;
  91.  
  92.      case TPNSGML:            /* Non-SGML character reference. */
  93.           location(10);
  94.           printf("Non-SGML character %d", te.nonsgml);
  95.           continue;
  96.  
  97.      case TPPCDATA:           /* Parsed character data found. */
  98.      case TPCDATA:            /* Character data entity reference. */
  99.      case TPSDATA:            /* System data entity reference. */
  100.           location(10);
  101.           printf("%d data characte%s%s: [",
  102.                  te.datalen,
  103.                  (rc==TPCDATA ? " in CDATA entity" : ""),
  104.                  (rc==TPSDATA ? " in SDATA entity" : ""));
  105.           printf("%.*s]", te.datalen, te.data);
  106.           continue;
  107.  
  108.      case TPSTAG:             /* Start-tag. */
  109.           location(11);
  110.           printf("%s start-tag found or implied.", te.data);
  111. /*        if (te.gidata) printf(" [GIDATA = \"%s\"]", te.gidata);*/
  112.           if (te.alcnt) getatts();
  113.           continue;
  114.  
  115.      case TPETAG:             /* End-tag found. */
  116.           location(12);
  117.           printf("%s end-tag found or implied.", te.data);
  118. /*        if (te.gidata) printf(" [GIDATA = \"%s\"]", te.gidata);*/
  119.           continue;
  120.  
  121.      case TPPI:               /* Processing instruction. */
  122.      case TPPIENT:            /* Processing instruction entity. */
  123.           location(13);
  124.           printf("%d character processing instruction%s: [%.*s]",
  125.                te.datalen, (rc==TPPIENT ? " in PI entity" : ""),
  126.                te.datalen, te.data);
  127.           continue;
  128.  
  129.      case TPRE:               /* Significant record end. */
  130.           location(14);
  131.           printf("Record end.");
  132.           continue;
  133.  
  134.      case TPEDTD:             /* Document type definition completed. */
  135.           location(16);       /* GISET and GIGET are now allowed. */
  136.           printf("Document type definition completed for %s; ", te.data);
  137.           /* Store GIDATA for element start-tags and end-tags here. */
  138.           /* GISET returns non-zero for undefined elements. */
  139. /*        giset("MODELGI",    /* Generic Identifier. */
  140. /*              strtproc,     /* Ptr to procedure for start-tag. */
  141. /*              endproc);     /* Ptr to procedure for start-tag. */
  142.           continue;
  143.  
  144.      default:
  145.           continue;
  146.      }
  147.      /*   TPEOD:                 End of document. */
  148.      return;
  149. }
  150. /******************************************************************************/
  151. /* GETATTS: Retrieve attribute specifications, one by one, and print them.
  152. */
  153. void getatts()
  154. {
  155.      while (alnext()) {
  156.           /* No value specified for #IMPLIED or #REQUIRED.
  157.           */
  158.           if (CA.astatus==TPAIMPLY || CA.astatus==TPANOREQ) {
  159.                printf("\n     %-8s = [NONE]", CA.aname);
  160.                if (CA.astatus==TPANOREQ) printf(" [INVALID]");
  161.                return;
  162.           }
  163.           /* Value specified or defaulted.
  164.           */
  165.           printf("\n");
  166.           printf("%-8s = \"", CA.aname);
  167.           switch (CA.atype) {
  168.           case TPALIST:            /* Value is list of tokens. */
  169.                avnext();           /* First token is on same line. */
  170.                printf("%.*s", CA.tokenlen, CA.token);
  171.                proctkn(CA.tokenlen, CA.token);
  172.                while (avnext()) {  /* New lines for any other tokens. */
  173.                     printf("\n");
  174.                     printf("            %.*s", CA.tokenlen, CA.token);
  175.                     proctkn(CA.tokenlen, CA.token);
  176.                }
  177.                printf("\"");
  178.                break;
  179.           case TPACDATA:           /* Value is string.*/
  180.           case TPATOKEN:           /* Value is single token.*/
  181.                printf("%.*s", CA.avallen, CA.aval);
  182.                proctkn(CA.avallen, CA.aval);
  183.                printf("\"");
  184.                break;
  185.           }
  186.           if (CA.astatus==TPAERROR) printf(" [ERROR]");
  187.           else if (CA.astatus==TPAINVAL) printf(" [INVALID]");
  188.           if (CA.aconref) printf(" [CONREF]");
  189.      }
  190. }
  191. /******************************************************************************/
  192. /* PROCTKN: Process a single token or list member according to its type.
  193. */
  194. void proctkn(tokenlen, token)
  195. unsigned tokenlen;            /* Length of token. */
  196. char *token;                  /* The token itself. */
  197. {
  198.      switch (CA.adata) {
  199.      case TPANOTE:                 /* dcnid, dcnidlen */
  200.           printf("=>%s", CA.dcnid);
  201.           break;
  202.      case TPAENTIT:                /* neid,dcnnm,dcnid+len; alcnt */
  203.           getent(tokenlen, token); /* Set up rcbde for this entity. */
  204.           getde();
  205.           break;
  206.      case TPAIDREF:                /* idrstat */
  207.           idrnext();
  208.           if (!CA.idrstat) printf(" [INVALID]");
  209.           break;
  210.      }
  211. }
  212. /******************************************************************************/
  213. /* GETDE: Get control block information for data entity named in
  214.           AENTITY attribute or external data entity reference.
  215. */
  216. void getde()
  217. {
  218.      printf("=>%s [type %d]", de.detxt, de.detype);
  219.      if (de.detype<=TPISDATA) return;
  220.      printf("; notation is %s=>%s.",
  221.           (de.dcnnm!=0) ? de.dcnnm   : "[UNDEFINED]",
  222.           (de.dcnid!=0) ? de.dcnid+2 : "[UNDEFINED]");
  223.      if (de.alcnt) getatts();
  224. }
  225. /******************************************************************************/
  226.